import socket

class TalkClient:
    def __init__(self):
        self.client = socket.socket() # 实例化socket对象

    def send_message(self):
        # 1. 建立与服务器端的连接
        # 2. 发送数据
        # 3. 关闭连接

        self.client.connect(('192.168.49.1', 8000))
        while True:
            to_msg = input("请输入你的消息：")
            self.client.send(to_msg.encode())    # 将一个字符串以特定的编码格式进行编码，默认是utf-8

            from_msg = self.client.recv(1024).decode()  # 接收来自服务器端的消息
            print(from_msg)

    def __del__(self):
        self.client.close()

if __name__ == '__main__':
    tc = TalkClient()
    tc.send_message()
